﻿using Microsoft.AspNetCore.Mvc;
using System.Linq;

namespace brand.admin.cyt369.com.Areas.BrandAdmin.Controllers
{
    public partial class CategoryController 
    {
        /// <summary>
        /// 获取类型通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetCategoryByID(int id)
        {
            var category=brand.admin.cyt369.com.services.Services.CategoryService.Instance.GetCategoryByID(id);
            return DefaultJson(category);
        }
        
        /// <summary>
        /// 获取类别通过 父级Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetCategoryByParentID(int id=0)
        {
            var category = brand.admin.cyt369.com.services.Services.CategoryService.Instance.GetCategoryByParentID(id).Select(o => new
            {
                o.Id,
                o.ParentID,
                o.CategoryName,
                o.Note,
                o.OrderNumber,
                o.IsDeleted,
                children = o.Childs,
                o.Childs
            });
            return DefaultJson(category);

        }


        /// <summary>
        /// 获取类别通过 父级Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetCategoryByParentID_NoInclude(int id = 0)
        {
            var category = brand.admin.cyt369.com.services.Services.CategoryService.Instance.GetCategoryByParentID(id).Select(o => new
            {
                o.Id,
                id=o.Id,
                o.ParentID,
                o.CategoryName,
                Name=o.CategoryName,
                text=o.CategoryName,
                o.Note,
                o.OrderNumber,
                o.IsDeleted,
            });
            return DefaultJson(category);

        }

        /// <summary>
        /// 获取所有类别
        /// </summary>
        /// <returns></returns>
        public IActionResult CategoryGets()
        {
            var category = brand.admin.cyt369.com.services.Services.CategoryService.Instance.CategoryGets();
            return DefaultJson(category);

        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SaveCategory(brand.admin.cyt369.com.services.Models.Category category)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (category != null && brand.admin.cyt369.com.services.Services.CategoryService.Instance.SaveCategory(category))
            {
                resp.Result = true;
                resp.Msg = System.Text.Json.JsonSerializer.Serialize(category);
            }
            else
            {
                resp.Msg = "保存失败";
            }
            return DefaultJson(resp);
        }

        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteCategory(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (brand.admin.cyt369.com.services.Services.CategoryService.Instance.DeleteCategory(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }
    }
}
